package com.example.calender.ui;

import android.content.Intent;
import android.graphics.Color;
import android.widget.TextView;

import com.example.calender.BaseActivity;
import com.example.calender.BaseApplication;
import com.example.calender.R;
import com.example.calender.utils.SharedPreferencesUtils;
import com.zackratos.ultimatebarx.library.UltimateBarX;
import com.zackratos.ultimatebarx.library.bean.BarConfig;

import java.util.Calendar;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;



public class SplashActivity extends BaseActivity {

    private Runnable runnableToLogin = new Runnable() {
        @Override
        public void run() {
            toLoginActivity();
        }
    };
    private TextView skip;
    private ScheduledFuture<?> scheduledFuture;
    private TextView tvTime;

    @Override
    protected int setLayout() {
        return R.layout.activity_splash;
    }

    @Override
    protected void findViews() {
        skip = findViewById(R.id.tv_splash_skip);
        tvTime = findViewById(R.id.tv_time);
    }

    @Override
    protected void initView() {
        Calendar ca = Calendar.getInstance();
        int day = ca.get(Calendar.DATE);
        UltimateBarX.with(this)
                .config(new BarConfig().fitWindow(false).color(Color.argb(0,255,255,255)).light(false))
                .applyStatusBar();
        BaseApplication instance = (BaseApplication)getApplication();
        scheduledFuture = instance.getThreadPool().schedule(runnableToLogin, 3, TimeUnit.SECONDS);
        skip.setOnClickListener(v -> {
            scheduledFuture.cancel(true);
            toLoginActivity();
        });
        tvTime.setText(day+"");
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    private void toLoginActivity(){
        Boolean is = (Boolean) SharedPreferencesUtils.getParam("isLogin", false);
        Intent intent;
        if (is) {
            intent = new Intent(this, MainActivity.class);
        } else {
            intent = new Intent(this, LoginActivity.class);
        }
        startActivity(intent);
        finish();
    }

    @Override
    protected void onStop() {
        super.onStop();
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        scheduledFuture.cancel(true);
    }
}